﻿package core.image
{
    import core.enum.*;
    import flash.display.*;
    import flash.geom.*;

    public class Reform extends Object
    {

        public function Reform()
        {
            return;
        }// end function

        public function Crop(core.image:Layer, core.image:Rectangle) : void
        {
            var _loc_3:* = undefined;
            var _loc_4:BitmapData = null;
            if (core.image.bitmap != null && core.image.bitmap.bitmapData != null)
            {
                _loc_3 = core.image.intersection(new Rectangle(core.image.x, core.image.y, core.image.width, core.image.height));
                if (_loc_3.width > 0 && _loc_3.height > 0)
                {
                    _loc_4 = new BitmapData(_loc_3.width, _loc_3.height, true, 16777215);
                    _loc_4.copyPixels(core.image.bitmap.bitmapData, new Rectangle(_loc_3.x - core.image.x, _loc_3.y - core.image.y, _loc_3.width, _loc_3.height), _loc_4.rect.topLeft);
                    core.image.bitmap.bitmapData = _loc_4;
                    if (core.image.hasMask && core.image.masking.bitmapData != null)
                    {
                        _loc_4 = new BitmapData(_loc_3.width, _loc_3.height, true, 16777215);
                        _loc_4.copyPixels(core.image.masking.bitmapData, new Rectangle(_loc_3.x - core.image.x, _loc_3.y - core.image.y, _loc_3.width, _loc_3.height), _loc_4.rect.topLeft);
                        core.image.masking.bitmapData = _loc_4;
                    }
                    core.image.x = _loc_3.x - core.image.x;
                    core.image.y = _loc_3.y - core.image.y;
                }
                else
                {
                    core.image.bitmap.bitmapData.dispose();
                    core.image.bitmap.bitmapData = null;
                }
            }
            return;
        }// end function

        public function Newsize(core.image:Reform:BitmapData, core.image:Reform:int, core.image:Reform:int, core.image:Reform:Point, core.image:Reform:uint) : BitmapData
        {
            var _loc_6:* = new BitmapData(core.image:Reform, core.image:Reform, true, core.image:Reform);
            _loc_6.copyPixels(core.image:Reform, core.image:Reform.rect, core.image:Reform);
            return _loc_6;
        }// end function

        public function MaxSize(core.image:Reform:BitmapData, core.image:Reform:int, core.image:Reform:int) : BitmapData
        {
            var _loc_4:int = 0;
            var _loc_5:int = 0;
            if (core.image:Reform.width > core.image:Reform || core.image:Reform.height > core.image:Reform)
            {
                if (core.image:Reform.height / core.image:Reform > core.image:Reform.width / core.image:Reform)
                {
                    _loc_4 = Math.round(core.image:Reform.width * (core.image:Reform / core.image:Reform.height));
                    _loc_5 = core.image:Reform;
                }
                else
                {
                    _loc_5 = Math.round(core.image:Reform.height * (core.image:Reform / core.image:Reform.width));
                    _loc_4 = core.image:Reform;
                }
                return this.Resize(core.image:Reform, _loc_4 / core.image:Reform.width, _loc_5 / core.image:Reform.height);
            }
            return core.image:Reform;
        }// end function

        public function Resize(core.image:Reform:BitmapData, core.image:Reform:Number, core.image:Reform:Number) : BitmapData
        {
            var _loc_4:Matrix = null;
            var _loc_5:BitmapData = null;
            var _loc_6:* = Math.round(core.image:Reform.width * core.image:Reform);
            var _loc_7:* = Math.round(core.image:Reform.height * core.image:Reform);
            while (core.image:Reform.width > _loc_6 * 2 && core.image:Reform.height > _loc_7 * 2)
            {
                
                _loc_5 = core.image:Reform.clone();
                core.image:Reform = new BitmapData(Math.round(core.image:Reform.width * 0.5), Math.round(core.image:Reform.height * 0.5), true, 16777215);
                _loc_4 = new Matrix();
                _loc_4.scale(core.image:Reform.width / _loc_5.width, core.image:Reform.height / _loc_5.height);
                core.image:Reform.draw(_loc_5, _loc_4, null, null, null, true);
            }
            _loc_4 = new Matrix();
            _loc_4.scale(_loc_6 / core.image:Reform.width, _loc_7 / core.image:Reform.height);
            var _loc_8:* = new Bitmap(core.image:Reform, "auto", true);
            _loc_5 = new BitmapData(_loc_6, _loc_7, true, 16777215);
            _loc_5.draw(_loc_8, _loc_4, null, null, null, true);
            return _loc_5;
        }// end function

        public function Flip(core.image:BitmapData, core.image:FlipType) : void
        {
            var _loc_3:int = 0;
            var _loc_4:int = 0;
            var _loc_5:* = core.image.width;
            var _loc_6:* = core.image.height;
            var _loc_7:* = new BitmapData(_loc_5, _loc_6, true, 16777215);
            if (core.image == FlipType.Vertical)
            {
                _loc_3 = 0;
                while (_loc_3 < _loc_5)
                {
                    
                    _loc_4 = 0;
                    while (_loc_4 < _loc_6)
                    {
                        
                        _loc_7.setPixel32(_loc_3, _loc_6 - _loc_4 - 1, core.image.getPixel32(_loc_3, _loc_4));
                        _loc_4++;
                    }
                    _loc_3++;
                }
            }
            else
            {
                _loc_3 = 0;
                while (_loc_3 < _loc_5)
                {
                    
                    _loc_4 = 0;
                    while (_loc_4 < _loc_6)
                    {
                        
                        _loc_7.setPixel32(_loc_5 - _loc_3 - 1, _loc_4, core.image.getPixel32(_loc_3, _loc_4));
                        _loc_4++;
                    }
                    _loc_3++;
                }
            }
            core.image.copyPixels(_loc_7, _loc_7.rect, _loc_7.rect.topLeft);
            return;
        }// end function

        public function Rotate(core.image:Reform:BitmapData, core.image:Reform:int) : BitmapData
        {
            var _loc_3:BitmapData = null;
            var _loc_4:int = 0;
            var _loc_5:int = 0;
            var _loc_6:* = core.image:Reform.width;
            var _loc_7:* = core.image:Reform.height;
            if (core.image:Reform == 180)
            {
                _loc_3 = new BitmapData(_loc_6, _loc_7, true, 16777215);
                _loc_4 = 0;
                while (_loc_4 < _loc_6)
                {
                    
                    _loc_5 = 0;
                    while (_loc_5 < _loc_7)
                    {
                        
                        _loc_3.setPixel32((_loc_6 - 1) - _loc_4, (_loc_7 - 1) - _loc_5, core.image:Reform.getPixel32(_loc_4, _loc_5));
                        _loc_5++;
                    }
                    _loc_4++;
                }
            }
            else if (core.image:Reform == 90)
            {
                _loc_3 = new BitmapData(_loc_7, _loc_6, true, 16777215);
                _loc_4 = 0;
                while (_loc_4 < _loc_6)
                {
                    
                    _loc_5 = 0;
                    while (_loc_5 < _loc_7)
                    {
                        
                        _loc_3.setPixel32((_loc_7 - 1) - _loc_5, _loc_4, core.image:Reform.getPixel32(_loc_4, _loc_5));
                        _loc_5++;
                    }
                    _loc_4++;
                }
            }
            else
            {
                _loc_3 = new BitmapData(_loc_7, _loc_6, true, 16777215);
                _loc_4 = 0;
                while (_loc_4 < _loc_6)
                {
                    
                    _loc_5 = 0;
                    while (_loc_5 < _loc_7)
                    {
                        
                        _loc_3.setPixel32(_loc_5, (_loc_6 - 1) - _loc_4, core.image:Reform.getPixel32(_loc_4, _loc_5));
                        _loc_5++;
                    }
                    _loc_4++;
                }
            }
            return _loc_3;
        }// end function

    }
}
